package com.xpec.c4.service.module.datacenter.account;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.DataManager;
import com.xpec.c4.game.datamanager.DataSource;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.datacenter.DataElement;


/**
 * 
 * @author yangbingyan
 *
 */
public class AccountDataManager extends DataManager<DataElement>{
	
	/**
	 * 
	 */
	@SuppressWarnings("unused")
	private static final Log log = LogFactory.getLog(AccountDataManager.class);

	/**
	 * 
	 * @param module
	 */
	public AccountDataManager(BasicServiceModule module) {
		super(module);
	}
	
	public void init(){
		
	}

	protected int createDataNoCheck(BaseDataInfo info, String srcModuleID, Object... userdata) {

		CreateAccountDataAsync asyncTask = new CreateAccountDataAsync();

		asyncTask.setDataManager(this);
		asyncTask.setDataInfo(info);
		asyncTask.setSrcModuleID((String) srcModuleID);
		asyncTask.setCallback(this);

		// 异步处理
		if(getModule().addAsyncCommand(asyncTask)){
			return ResultNo.PENDING;
		}else{
			return ResultNo.FAILED;
		}
	}
	
	/**
	 * 
	 * @param baseInfo
	 * @param srcModuleID
	 * @param userdata
	 * @return
	 */
	protected int readDataAsync(BaseDataInfo info, String srcModuleID,
			Object... userdata) {
		
		// 前面如果沒有return...就會走數據從DB讀取的流程
		LoadAccountDataAsync loadData = new LoadAccountDataAsync();

		loadData.setDataManager(this);
		loadData.setDataInfo(info);
		loadData.setSrcModuleID((String) srcModuleID);
		loadData.setCallback(this);
		loadData.key = info.getKey();

		// 异步处理
		getModule().addAsyncCommand(loadData);
		
		return ResultNo.PENDING;
	}

	public void createDataCallback(int resultCode, BaseDataInfo info,
			String srcModuleID, Object resultObj) {	
		
		info.setResultCode(resultCode);
		info.setData(null);
		dataResponse(info, srcModuleID);
	}
	
	/**
	 * 從DB回來會被調用的地方<br>
	 * @param resultCode 返回值、處理結果
	 * @param info 原本放到task的資料
	 * @param srcModuleID 請求人ID
	 * @param obj 從DB撈回來的資料
	 * @param dataResponse 是否需要call dataResponse(..)
	 */
	public void readDataCallback(int resultCode, BaseDataInfo info,
			String srcModuleID, Object resultObj, boolean dataResponse) {		
		
		if(resultCode == ResultNo.OK && resultObj != null){
			
			info.setDataSource(DataSource.DATABASE);
			info.setResultCode(resultCode);
			info.setData(resultObj);
			if(dataResponse) dataResponse(info, srcModuleID);
		}
		else{			
			// 读取错误
			info.setResultCode(resultCode);
			if(dataResponse){
				dataResponse(info, srcModuleID);
			}
			
			log.error("DB Load failed:" + info.getKey().toString() );
		}
	}
	
	/**
	 * 
	 * @param key
	 * @param userdata
	 * @return
	 */
	public int writeData(BaseDataInfo info, String srcModuleID, Object ...userdata){
		
		SaveAccountDataAsync saveData = new SaveAccountDataAsync();
		saveData.setDataManager(this);
		saveData.setDataInfo(info);
		saveData.setSrcModuleID((String) srcModuleID);
		saveData.setCallback(this);
		saveData.key = info.getKey();

		// 异步处理
		getModule().addAsyncCommand(saveData);
		return ResultNo.PENDING;	
	}
	

	/**
	 * 
	 * @param resultCode
	 * @param info
	 * @param srcModuleID
	 * @param obj
	 */
	public void writeDataCallback(int resultCode, BaseDataInfo info,
			String srcModuleID, Object obj) {
		
		info.setDataSource(DataSource.UNKNOW);
		info.setResultCode(resultCode);
		// 清掉原本的數據不用返回
		info.setData(null);
		dataResponse(info, srcModuleID);
		
	}
	
}
